iT邦幫忙

2021 iThome 鐵人賽

DAY 18
0
AI & Data

想到甚麼打甚麼系列 第 18

找LeetCode上簡單的題目來撐過30天啦(DAY18)

  • 分享至 

  • xImage
  •  

我越來越懂linklist了(應該吧?),可喜可賀

題號:21 標題:Merge Two Sorted Lists 難度:Easy

Merge two sorted linked lists and return it as a sorted list. The list should be made by splicing together the nodes of the first two lists.

Example 1:

Input: l1 = [1,2,4], l2 = [1,3,4]
Output: [1,1,2,3,4,4]
Example 2:
Input: l1 = [], l2 = []
Output: []
Example 3:
Input: l1 = [], l2 = [0]
Output: [0]

Constraints:
• The number of nodes in both lists is in the range [0, 50].
• -100 <= Node.val <= 100
• Both l1 and l2 are sorted in non-decreasing order.

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution {
    public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
        ListNode result = new ListNode();
        ListNode cur = new ListNode();

        ListNode temp = new ListNode();
        int start = 0;
        if(l1 == null && l2==null){
            return l1;
        }
        while(l1 != null || l2!= null){
            //System.out.println("v1:" + l1.val + "  v2:" + l2.val);
            if(l1 == null){
                if(start == 0){
                    result = l2;
                    cur = l2;
                    temp = l2;
                    System.out.println("1:" + cur.val);
                    l2 = l2.next;
                    start++;
                }else{
                    temp.next = l2;
                    System.out.println("2:" + l2.val);
                    cur = temp.next;
                    temp = l2;
                    l2 = l2.next;
                }
            }else if(l2 == null){
                if(start == 0){
                    result = l1;
                    cur = l1;
                    temp = l1;
                    System.out.println("3:" + cur.val);
                    l1 = l1.next;
                    start++;
                }else{
                    temp.next = l1;
                    cur = temp.next;
                    temp = cur;
                    System.out.println("4:" + cur.val);
                    l1 = l1.next;
                }
            }else{
                if(l1.val <= l2.val && start == 0){
                    result = l1;
                    cur = l1;
                    temp = l1;
                    System.out.println("5:" + cur.val);
                    l1 = l1.next;
                    start++;
                }else if(l2.val < l1.val && start == 0){
                    result = l2;
                    cur = l2;
                    temp = l2;
                    System.out.println("7:" + cur.val);
                    l2 = l2.next;
                    start++;
                }else if(l1.val <= l2.val ){
                    temp.next = l1;
                    cur = temp.next;
                    temp = cur;
                    System.out.println("6:" + cur.val);
                    l1 = l1.next;
                }else{
                    temp.next = l2;
                    System.out.println("8:" + cur.val);
                    cur = temp.next;
                    temp = cur;
                    l2 = l2.next;                
                }
            }
            
        }
        
        return result;
    }
}

題號:205 標題:Isomorphic Strings 難度:Easy

Given two strings s and t, determine if they are isomorphic.
Two strings s and t are isomorphic if the characters in s can be replaced to get t.
All occurrences of a character must be replaced with another character while preserving the order of characters. No two characters may map to the same character, but a character may map to itself.

Example 1:
Input: s = "egg", t = "add"
Output: true


Example 2:
Input: s = "foo", t = "bar"
Output: false


Example 3:
Input: s = "paper", t = "title"
Output: true


Constraints:
• 1 <= s.length <= 5 * 104
• t.length == s.length
• s and t consist of any valid ascii character.


bool isIsomorphic(char * s, char * t){
    int len=strlen(s),i,j;
    //printf("%d,%d",lens,lent);
    int temp[128]={0};
    for(i=0;i<len;i++){
        if(temp[s[i]]==0){
            temp[s[i]] = t[i];
        }else if(temp[s[i]] != t[i]){
            return false;
        }
    }
    int temp2[128]={0};
    for(i=0;i<len;i++){
        if(temp2[t[i]]==0){
            temp2[t[i]] = s[i];
        }else if(temp2[t[i]] != s[i]){
            return false;
        }
    }

       
    return true;
}

DAY18心得
我有進步的感覺,開心/images/emoticon/emoticon12.gif


上一篇
找LeetCode上簡單的題目來撐過30天啦(DAY17)
下一篇
找LeetCode上簡單的題目來撐過30天啦(DAY19)
系列文
想到甚麼打甚麼30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言